<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */
namespace Admin\Controller;
use Admin\Logic\ArticleCatLogic;

class ArticleController extends BaseController {

	public function categoryList() {
		$ArticleCat = new ArticleCatLogic();
		$cat_list = $ArticleCat -> article_cat_list(0, 0, false);
		$type_arr = array('系统默认', '系统帮助', '系统公告');
		$this -> assign('type_arr', $type_arr);
		$this -> assign('cat_list', $cat_list);
		$this -> display('categoryList');
	}

	public function category() {
		$ArticleCat = new ArticleCatLogic();
		$act = I('GET.act', 'add');
		$this -> assign('act', $act);
		$cat_id = I('GET.cat_id');
		$cat_info = array();
		if ($cat_id) {
			$cat_info = D('article_cat') -> where('cat_id=' . $cat_id) -> find();
			$this -> assign('cat_info', $cat_info);
		}
		$cats = $ArticleCat -> article_cat_list(0, $cat_info['parent_id'], true);
		$this -> assign('cat_select', $cats);
		$this -> display();
	}

	public function articleList() {
		$Article = M('Article');
		$res = $list = array();
		$p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
		$size = empty($_REQUEST['size']) ? 20 : $_REQUEST['size'];

		$where = " 1 = 1 ";
		$keywords = trim(I('keywords'));
		$keywords && $where .= " and title like '%$keywords%' ";
		$cat_id = I('cat_id', 0);
		$cat_id && $where .= " and cat_id = $cat_id ";
		$res = $Article -> where($where) -> order('article_id desc') -> page("$p,$size") -> select();
		$count = $Article -> where($where) -> count();
		// 查询满足要求的总记录数
		$pager = new \Think\Page($count, $size);
		// 实例化分页类 传入总记录数和每页显示的记录数
		$page = $pager -> show();
		//分页显示输出

		$ArticleCat = new ArticleCatLogic();
		$cats = $ArticleCat -> article_cat_list(0, 0, false);
		if ($res) {
			foreach ($res as $val) {
				$val['category'] = $cats[$val['cat_id']]['cat_name'];
				$val['add_time'] = date('Y-m-d H:i:s', $val['add_time']);
				$list[] = $val;
			}
		}
		$this -> assign('cats', $cats);
		$this -> assign('cat_id', $cat_id);
		$this -> assign('list', $list);
		// 赋值数据集
		$this -> assign('page', $page);
		// 赋值分页输出
		$this -> display('articleList');
	}

	public function article() {
		$ArticleCat = new ArticleCatLogic();
		$act = I('GET.act', 'add');
		$info = array();
		$info['publish_time'] = time() + 3600 * 24;
		if (I('GET.article_id')) {
			$article_id = I('GET.article_id');
			$info = D('article') -> where('article_id=' . $article_id) -> find();
		}
		$cats = $ArticleCat -> article_cat_list(0, $info['cat_id']);
		$this -> assign('cat_select', $cats);
		$this -> assign('act', $act);
		$this -> assign('info', $info);
		$this -> initEditor();
		$this -> display();
	}

	/**
	 * 初始化编辑器链接
	 * @param $post_id post_id
	 */
	private function initEditor() {
		$this -> assign("URL_upload", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
		$this -> assign("URL_fileUp", U('Admin/Ueditor/fileUp', array('savepath' => 'article')));
		$this -> assign("URL_scrawlUp", U('Admin/Ueditor/scrawlUp', array('savepath' => 'article')));
		$this -> assign("URL_getRemoteImage", U('Admin/Ueditor/getRemoteImage', array('savepath' => 'article')));
		$this -> assign("URL_imageManager", U('Admin/Ueditor/imageManager', array('savepath' => 'article')));
		$this -> assign("URL_imageUp", U('Admin/Ueditor/imageUp', array('savepath' => 'article')));
		$this -> assign("URL_getMovie", U('Admin/Ueditor/getMovie', array('savepath' => 'article')));
		$this -> assign("URL_Home", "");
	}

	public function categoryHandle() {

		$data = I('post.');

		if ($data['act'] == 'add') {
			$d = D('article_cat') -> add($data);
		}

		if ($data['act'] == 'edit') {
			if ($data['cat_id'] == $data['parent_id']) {
				$this -> error("所选分类的上级分类不能是当前分类", U('Admin/Article/category', array('cat_id' => $data['cat_id'])));
			}
			$ArticleCat = new ArticleCatLogic();
			$children = array_keys($ArticleCat -> article_cat_list($data['cat_id'], 0, false));
			// 获得当前分类的所有下级分类
			if (in_array($data['parent_id'], $children)) {
				$this -> error("所选分类的上级分类不能是当前分类的子分类", U('Admin/Article/category', array('cat_id' => $data['cat_id'])));
			}
			$d = D('article_cat') -> where("cat_id=$data[cat_id]") -> save($data);

			if ($_FILES) {
				// 上传图片的处理
				$cfg = array('rootPath' => './Public/upload/jj_newsPhoto/', 'autoSub' => false, );
				$upload = new \Think\Upload($cfg);
				$result = $upload -> uploadOne($_FILES['banner']);
				// 获得路径
				$return = $upload -> rootPath . $result['savepath'] . $result['savename'];
				// 去掉第一个 点
				$result_str = substr($return, 1);

				$update['photo'] = $result_str;
				$photo = M('article_cat');

				$d = $photo -> where('cat_id=' . $data['cat_id']) -> save($update);
			}

		}

		if ($data['act'] == 'del') {
			$res = D('article_cat') -> where('parent_id =' . $data['cat_id']) -> select();
			if ($res) {
				exit(json_encode('还有子分类，不能删除'));
			}
			$res = D('article') -> where('cat_id =' . $data['cat_id']) -> select();
			if ($res) {
				exit(json_encode('非空的分类不允许删除'));
			}
			$r = D('article_cat') -> where('cat_id=' . $data['cat_id']) -> delete();
			if ($r)
				exit(json_encode(1));
		}
		if ($d) {
			$this -> success("操作成功", U('Admin/Article/categoryList'));
		} else {
			$this -> error("操作失败", U('Admin/Article/categoryList'));
		}
	}

	public function aticleHandle() {
		$data = I('post.');
		$data['publish_time'] = strtotime($data['publish_time']);
		//$data['content'] = htmlspecialchars(stripslashes($_POST['content']));
		if ($data['act'] == 'add') {
			$data['click'] = mt_rand(1000, 1300);
			$data['add_time'] = time();
			$r = D('article') -> add($data);
		}

		if ($data['act'] == 'edit') {
			$r = D('article') -> where('article_id=' . $data['article_id']) -> save($data);
		}

		if ($data['act'] == 'del') {
			$r = D('article') -> where('article_id=' . $data['article_id']) -> delete();
			if ($r)
				exit(json_encode(1));
		}
		$referurl = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : U('Admin/Article/articleList');
		if ($r) {
			$this -> success("操作成功", $referurl);
		} else {
			$this -> error("操作失败", $referurl);
		}
	}

	public function link() {
		$act = I('GET.act', 'add');
		$this -> assign('act', $act);
		$link_id = I('GET.link_id');
		$link_info = array();
		if ($link_id) {
			$link_info = D('friend_link') -> where('link_id=' . $link_id) -> find();
			$this -> assign('info', $link_info);
		}
		$this -> display();
	}

	public function linkList() {
		$Ad = M('friend_link');
		$res = $Ad -> where('1=1') -> order('orderby') -> page($_GET['p'] . ',10') -> select();
		if ($res) {
			foreach ($res as $val) {
				$val['target'] = $val['target'] > 0 ? '开启' : '关闭';
				$list[] = $val;
			}
		}
		$this -> assign('list', $list);
		// 赋值数据集
		$count = $Ad -> where('1=1') -> count();
		// 查询满足要求的总记录数
		$Page = new \Think\Page($count, 10);
		// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page -> show();
		// 分页显示输出
		$this -> assign('page', $show);
		// 赋值分页输出
		$this -> display();
	}

	public function linkHandle() {
		$data = I('post.');
		if ($data['act'] == 'add') {
			stream_context_set_default(array('http' => array('timeout' => 2)));
			send_http_status('311');
			$r = D('friend_link') -> add($data);
		}
		if ($data['act'] == 'edit') {
			$r = D('friend_link') -> where('link_id=' . $data['link_id']) -> save($data);
		}

		if ($data['act'] == 'del') {
			$r = D('friend_link') -> where('link_id=' . $data['link_id']) -> delete();
			if ($r)
				exit(json_encode(1));
		}

		if ($r) {
			$this -> success("操作成功", U('Admin/Article/linkList'));
		} else {
			$this -> error("操作失败", U('Admin/Article/linkList'));
		}
	}

	//合作伙伴 东
	public function help_list() {
		$partners = D('partners');
		$partnersarr = $partners -> select();
		$arr = array();
		foreach ($partnersarr as $k => $v) {

			$arr[$v['id']] = substr($v['img'], 1);
		}
		$this -> assign('partnersarr', $arr);
		$this -> display();
	}

	//图片保存数据库  合作伙伴 东
	public function help_list_add() {
		if ($_POST) {
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
				$this -> error($upload -> getError());

			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}
			$partners = D('partners');

			$arr = array('img' => $dizi);
			$id = $partners -> add($arr);
			if ($id) {
				$this -> success('新增成功', 'help_list');
			}
		}
	}

	//图片删除数据库  合作伙伴 东
	public function help_list_delete() {
		$partners = D('partners');
		$id = $partners -> delete($_POST['id']);

		if ($id) {
			echo 1;
		}

	}
//操作删除后跳转回合作伙伴页面
	public function skip() {
		return $this -> success('提交成功', 'help_list', 3);

	}

	public function notice_list() {

	}

	//服务内容 东
	public function service_content() {
		$service = D('service_content');
//		if ($_POST) {
//
//			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );
//
//			$upload = new \Think\Upload($cfg);
//			// 图片上传
//			$result = $upload -> upload($_FILES);
//			$bigpathname = $upload -> rootPath;
//
//			if (!$result) {// 上传错误提示错误信息
//				$this -> error($upload -> getError());
//
//			} else {// 上传成功 获取上传文件信息
//				foreach ($result as $file) {
//					//图片地址
//					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
//				}
//			}
//
//			$arr = array('img' => $dizi, 'type' => $_POST['fname'], 'type_describe' => $_POST['DIVCSS5']);
//			//print_r($arr);
//			$id = $service -> add($arr);
//			if ($id) {
//				$this -> success('新增成功', 'service_content');
//			}
//		} else {
				
			// 这个界面就是一个 显示内容
			$service = $service -> select();
			foreach ($service as $k => $v) {
				$arr[$k] = $v;
				$arr[$k]['img'] = substr($v['img'], 1);
			}
			
			$this -> assign('service', $arr);
			$this -> display();

//		}
	}

	public function add_serviceContent(){
		$service = D('service_content');
		$this -> initEditor();
		if ($_POST) {
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
				$this -> error($upload -> getError());

			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array('img' => $dizi, 'type' => $_POST['title'], 'type_describe' => $_POST['content'],'content' => $_POST['contents']);
			$id = $service -> add($arr);
			if ($id) {
				$this -> success('新增成功', 'service_content');
			}
		}else{
			$this -> initEditor();
			$this->display();
		}
	}
	//服务内容 删除    东
	public function service_content_delete() {
		$service = D('service_content');
		$id = $service -> delete($_POST['id']);

		if ($id) {
			echo 1;
			//$this -> success('新增成功', 'service_content');
		}

	}
	//操作后跳转回服务内容页面
	public function skip_service_content() {
		return $this -> success('提交成功', 'service_content', 3);

	}
	//服务内容更新     东
	public function service_content_save($id) {

		$this -> initEditor();

		$service = D('service_content');
		if($_POST){
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
					
					$servicearr	= $service -> where("id = {$id}") -> find();
					$dizi =	$servicearr['img'];
			} else {// 上传成功 获取上传文件信息	
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array('img' => $dizi, 'type' => $_POST['company_name'], 'type_describe' => $_POST['company_introduce'],'content' => $_POST['contents']);
			$id = $service -> where("id = {$id}") ->save($arr);
			if($id){
				$this -> success('新增成功', U('Admin/Article/service_content'));
			}
		}else{
		
		$servicearr	= $service -> where("id = {$id}") -> find();
		$servicearr['img'] = substr($servicearr['img'], 1);
		$this -> assign('info', $servicearr);

		$this -> display();
				
	}
	}
	//杰匠企业  显示于保存 东
	public function jj_enterprise() {
		$jj_enterprise = D('jj_enterprise');
		if ($_POST) {
	
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
				$this -> error($upload -> getError());

			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array('img' => $dizi, 'name' => $_POST['name'], 'qiyejieshao' => $_POST['content'],'qiyename' => $_POST['qiyename'],'content' => $_POST['contents']);
			
			$id = $jj_enterprise -> add($arr);
			
			if ($id) {
				$this -> success('新增成功', 'jj_enterprise');
			}
				} else {

			$jj_enterprise = $jj_enterprise -> select();
			foreach ($jj_enterprise as $k => $v) {
				$arr[$k] = $v;
				$arr[$k]['img'] = substr($v['img'], 1);
			}
			
			$this -> assign('jj_enterprise', $arr);
			$this -> display();

		}
		
	}
////杰匠企业  删除 东
	public function jj_enterprise_delete() {
	$jj_enterprise = D('jj_enterprise');
		$id = $jj_enterprise -> delete($_POST['id']);
		if ($id) {
			echo 1;
		}
		
	}
	//操作后跳转回杰匠企业页面
	public function skip_jj_enterprise() {
		return $this -> success('提交成功', 'jj_enterprise', 3);

	}
//	/杰匠企业  修改 东 
	public function jj_enterprise_save($id) {
		
		$enterprise = D('jj_enterprise');
		if($_POST){
			
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
					
					$servicearr	= $enterprise -> where("id = {$id}") -> find();
					$dizi =	$servicearr['img'];
			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array('img' => $dizi, 'name' => $_POST['company_name'], 'qiyename' => $_POST['company_introduce'],'qiyejieshao' => $_POST['company_introduce2'],'content' => $_POST['contents']);
			$id = $enterprise -> where("id = {$id}") ->save($arr);
			
			if($id){
				$this -> success('新增成功', U('Admin/Article/jj_enterprise'));
			}
		}else{
		
		$enterprisearr	= $enterprise -> where("id = {$id}") -> find();
		$enterprisearr['img'] = substr($enterprisearr['img'], 1);
		$this -> initEditor();
		$this -> assign('info', $enterprisearr);
		$this -> display();
				
	}
			
	}
	//杰匠园区 显示与保存
	public function jj_park() {
		$jj_park = D('jj_park');

			$jj_park = $jj_park -> select();
			foreach ($jj_park as $k => $v) {
				$arr[$k] = $v;
				$arr[$k]['img'] = substr($v['img'], 1);
			}

			$this -> assign('jj_park', $arr);
			$this -> display();
	}
	
	public function add_jjParck(){
		$jj_park = D('jj_park');
		if ($_POST) {
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
				$this -> error($upload -> getError());

			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array('img' => $dizi, 'wenhua' => $_POST['title'], 'jianjie' => $_POST['content']);
			$id = $jj_park -> add($arr);
			if ($id) {
				$this -> success('新增成功', 'jj_park');
			}
		}else{
			$this->display();
		}
	}
	
////杰匠园区 删除
	public function jj_park_delete() {
		$jj_park = D('jj_park');
		$id = $jj_park -> delete($_POST['id']);
		if ($id) {
			echo 1;
		}
		
	}
//杰匠园区 修改 东 
	public function jj_park_save($id) {
	
		$jj_park = D('jj_park');
		if($_POST){
			
			$wenhua = $_POST['company_name'];
			$jianjie = $_POST['company_introduce'];
			
			$cfg = array('rootPath' => './Public/upload/jj_upload/', 'autoSub' => false, );

			$upload = new \Think\Upload($cfg);
			// 图片上传
			$result = $upload -> upload($_FILES);
			$bigpathname = $upload -> rootPath;

			if (!$result) {// 上传错误提示错误信息
					$servicearr	= $jj_park -> where("id = {$id}") -> find();
					$dizi =	$servicearr['img'];
			} else {// 上传成功 获取上传文件信息
				foreach ($result as $file) {
					//图片地址
					$dizi = $bigpathname . $file['savepath'] . $file['savename'];
				}
			}

			$arr = array(
				'img'=>$dizi,
				'wenhua'=>$wenhua,
				'jianjie'=>$jianjie,
			);
			$z = $jj_park->where('id='.$id)->save($arr); 
			if($z){
				$this -> success('新增成功',U('Admin/Article/jj_park'));
			}
		}else{
		
			$jj_parkarr	= $jj_park -> where("id = {$id}") -> find();
			$jj_parkarr['img'] = substr($jj_parkarr['img'], 1);
			$this -> assign('info', $jj_parkarr);
			$this -> display();
				
	}
			
	}
//操作删除后跳转回杰匠园区页面
	public function skip_jj_park() {
		return $this -> success('提交成功', 'jj_park', 3);

	}
	//订单基数 显示
	public function order_base(){
		$order_base = D('order_base');
		$basebrr = $order_base ->select();
		$this -> assign('basebrr', $basebrr);
		$this -> display();
	}
	//订单基数 
	public function order_base_save($id){
		$order_base = D('order_base');

		if($_POST){
			$arr  = array('base' => $_POST['base']);
		
			$id = $order_base -> where("base ='{$id}' ") ->save($arr);
			if($id){
				$this -> success('新增成功',U('Admin/Article/order_base'));
			}
		}else{
		$basebrr = $order_base -> where("base ='{$id}' ") ->find();
		$this -> assign('basebrr', $basebrr);
		$this -> display();
	}
}
	public function diaozhuanbase(){
			return $this -> success('提交成功', 'order_base' );
	}

}
